/**
 * 
 */
package Client.View;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import utils.FixedLengthDocument;
import utils.ImagePanel;

import logic.ChatMessage;
import logic.OnlinePlayer;

import Server.Player;

import Client.Model.GameModel;

/**
 * @author Rodrigo
 * 
 */

@SuppressWarnings("serial")
public class ConnectedView extends JPanel implements Observer{
	
	
	
	public static HashMap<Integer, String> stateMap = new HashMap<Integer, String>();
	
	static{	
		stateMap.put(new Integer(Player.STATE_CONNECTED), "Connected");
		stateMap.put(new Integer(Player.STATE_NONE), "Invalid");
		stateMap.put(new Integer(Player.STATE_PLAYING), "Playing");
		stateMap.put(new Integer(Player.STATE_WAITING), "Waiting");
	}
	
	
	private GameModel model;
	int width, height;
	
	private JButton logoutBtn, waitingBtn, sendMessageBtn;
	
	private JTextArea messageArea;
	private JPanel leftPanel, rightPanel, chatMessageBox;
	private JPanel  topPanel, logoPanel, topRightPanel, usernamePanel;
	JLabel usernameLbl;
	MyChatPane chat;
	OnlineUsersPane users;
	
	
	public ConnectedView(GameModel m, int height, int width) {
		
		this.width = width;
		this.height = height;
		this.model = m;
					
		model.addObserver(this);
		
		setBounds(0,0,width,height);
		
		setView();
		setListeners();
		
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
				
		if (model.getView()==GameModel.VIEW_CONNECTED){
			
			this.setVisible(true);
			
			synchronized (model) {

				if (model.isWaiting())
					waitingBtn.setText("Unmark as Waiting");
				else
					waitingBtn.setText("Mark as Waiting");
					

				usernameLbl.setText("Logged as: " + model.getUsername());

				/*--- Update Connected Users ---*/
				
				users.clear();

				for (OnlinePlayer p: model.getOnlinePlayers())
					users.addPlayerObject(new PlayerObject(p, model));



				/*--- Update Messages ---*/
				
				for (ChatMessage msg : model.getMessages())
					chat.addMessage(msg.getFrom(), msg.getMsg());
				
				model.getMessages().clear();
			}
		
		}
		else
			this.setVisible(false);
		
		revalidate();
		repaint();
		
	}
	
	private void setView(){
				
		//inicialize components
		usernameLbl = new JLabel();
		usernamePanel = new JPanel();
		
		usernamePanel.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		usernamePanel.setPreferredSize(new Dimension(180, 80));
		
		waitingBtn = new JButton("Mark as Waiting!");
		logoutBtn = new JButton("Logout");
		sendMessageBtn = new JButton("Send");			
		
		topPanel = new JPanel();
		logoPanel = new JPanel();
		topRightPanel = new JPanel();
		
		rightPanel = new JPanel();
		chatMessageBox = new JPanel();
		chat = new MyChatPane(width/2 - 5, 350);
		messageArea = new JTextArea(4, 20);
		messageArea.setWrapStyleWord(true);
		messageArea.setLineWrap(true);
		messageArea.setDocument(new FixedLengthDocument(100));
		
		leftPanel = new JPanel(); 
		users = new OnlineUsersPane(width/2 - 2, 450);	
		
		chatMessageBox.setPreferredSize(new Dimension(width/2, 60));
		chatMessageBox.setBackground(LoginFrame.DEFAULT_BG_COLOR);		
		
		topPanel.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		topPanel.setBounds(0, 0, width, 80);
		
		leftPanel.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		leftPanel.setBounds(0, 80, width/2, height-80);
		
		rightPanel.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		rightPanel.setBounds(width/2, 80, width/2, height-80);
		
		logoPanel.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		logoPanel.setPreferredSize(new Dimension(300, 80));
		
		topRightPanel.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		
		
		//----- Set Layout-----
		
		this.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		this.setLayout(null);	
		
		this.add(topPanel);
		this.add(leftPanel);
		this.add(rightPanel);
		
		topPanel.add(logoPanel);
		topPanel.add(topRightPanel);
		
		logoPanel.add(new ImagePanel("C:\\Users\\Rodrigo\\Dropbox\\Trabalhos\\PDist\\Partilha PDist\\logo.png", 0));
		
		topRightPanel.add(waitingBtn);
		topRightPanel.add(usernamePanel);
		
		usernamePanel.add(usernameLbl);
		usernamePanel.add(logoutBtn);
		
		leftPanel.add(new JLabel("Users"));
		leftPanel.add(users);
	
		
		rightPanel.add(new JLabel("Chat"));
		rightPanel.add(chat);
		rightPanel.add(chatMessageBox);
		
		chatMessageBox.add(messageArea);
		chatMessageBox.add(sendMessageBtn);
				
	}
	
	private void setListeners(){ 
		
		waitingBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				model.switchWaiting();
			}
		});
		
		logoutBtn.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				model.doLogout();				
			}
		});
		
		sendMessageBtn.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				
				model.sendMessage(messageArea.getText());
				
			}
			
		});
		
	}
}
